//
//  YHSearchBar.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/19.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

class YHSearchBar: UIView {

    @IBOutlet weak var searchTF: UITextField!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var rightConstant: NSLayoutConstraint!
    
   class func loadSearchBar() -> YHSearchBar{
       let searchView = NSBundle.mainBundle().loadNibNamed("YHSearchBar", owner: nil, options: nil).last as! YHSearchBar
        return searchView
    }
    
    override func awakeFromNib() {
        self.bounds.size.width = screenWidth
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        searchTF.leftViewMode = .Always
        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        searchTF.leftView?.contentMode = .Center
    }
    @IBAction func textFiledBeginEditting(sender: AnyObject) {
        
        UIView.animateWithDuration(0.25) { 
            
            self.rightConstant.constant = self.cancelBtn.bounds.width
            
            self.layoutIfNeeded()
        }
    }
    @IBAction func cancelBtnClick(sender: AnyObject) {
        
       
        UIView.animateWithDuration(0.25) {
            
             self.rightConstant.constant = 0
            
             self.layoutIfNeeded()
        }
        
        self.endEditing(true)
        
    }
}
